package com.ing5.BL.android.cinece;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.ing5.BL.android.cinece.api.model.Information;

public class SQLFavorite extends SQLiteOpenHelper {

	public static final int DATABASE_VERSION = 7;
	public static final String DATABASE_NAME = "Favorites.db";

	public static final String TABLE_NAME = "favorites";
	public static final String _ID = "_id";
	public static final String NAME_TITLE = "title";
	public static final String NAME_YEAR = "year";
	public static final String NAME_RATED = "rated";
	public static final String NAME_RELEASED = "released";
	public static final String NAME_RUNTIME = "runtime";
	public static final String NAME_GENRE = "genre";
	public static final String NAME_DIRECTOR = "director";
	public static final String NAME_WRITER = "writer";
	public static final String NAME_ACTORS = "actors";
	public static final String NAME_PLOT = "plot";
	public static final String NAME_POSTER = "imageUrl";
	public static final String NAME_RATING = "rating";
	public static final String NAME_VOTES = "votes";
	public static final String NAME_ID = "idimbd";
	public static final String NAME_TYPE = "type";

	private static final String SQL_CREATE_ENTRIES = "CREATE TABLE "
			+ TABLE_NAME + " ("	+ _ID + " INTEGER PRIMARY KEY,"
			+ NAME_TITLE + " TEXT,"
			+ NAME_YEAR	+ " TEXT,"
			+ NAME_RATED + " TEXT,"
			+ NAME_RELEASED + " TEXT,"
			+ NAME_RUNTIME + " TEXT,"
			+ NAME_GENRE + " TEXT,"
			+ NAME_DIRECTOR + " TEXT,"
			+ NAME_WRITER + " TEXT,"
			+ NAME_ACTORS + " TEXT,"
			+ NAME_PLOT + " TEXT,"
			+ NAME_POSTER + " TEXT,"
			+ NAME_RATING + " TEXT,"
			+ NAME_VOTES + " TEXT,"
			+ NAME_ID + " TEXT,"
			+ NAME_TYPE + " TEXT);";

	private static final String[] ALL_COLUMNS = { NAME_TITLE, NAME_YEAR,
			NAME_RATED, NAME_RELEASED, NAME_RUNTIME, NAME_GENRE, NAME_DIRECTOR,
			NAME_WRITER, NAME_ACTORS, NAME_PLOT, NAME_POSTER, NAME_RATING,
			NAME_VOTES, NAME_ID, NAME_TYPE, _ID };

	private static final String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS "
			+ TABLE_NAME;

	public SQLFavorite(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(SQL_CREATE_ENTRIES);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL(SQL_DELETE_ENTRIES);
		onCreate(db);
	}

	public List<Information> getAllMovies() {
		SQLiteDatabase db = null;
		List<Information> result = null;
		try {
			result = new ArrayList<Information>();
			db = getWritableDatabase();
			Cursor cursor = db.query(TABLE_NAME, ALL_COLUMNS, null, null, null,
					null, null);
			cursor.moveToFirst();
			while (!cursor.isAfterLast()) {
				result.add(cursorToInfo(cursor));
				cursor.moveToNext();
			}
			return result;
		} catch(SQLException e) {
			return null;
		} finally {
			db.close();
		}		
	}

	public Information getMovie(String idimdb) {
		SQLiteDatabase db = null;
		try {
			db = getWritableDatabase();
			Cursor cursor = db.query(TABLE_NAME, ALL_COLUMNS, null, null, null,
					null, null);
			cursor.moveToFirst();
			while (!cursor.isAfterLast()) {
				if(cursor.getString(13).equals(idimdb)) {
					return cursorToInfo(cursor);
				}
				cursor.moveToNext();
			}
			return null;
		} catch (SQLException e) {
			return null;
		} finally {
			db.close();
		}
	}

	public boolean addMovie(Information info) {
		SQLiteDatabase db = null;
		try {
			db = getWritableDatabase();
			db.insert(TABLE_NAME, null, infoToValues(info));
			return true;
		} catch (SQLException e) {
			return false;
		} finally {
			db.close();
		}
	}

	public boolean deleteMovie(Information info) {
		SQLiteDatabase db = null;
		try {
			db = getWritableDatabase();
			Cursor cursor = db.query(TABLE_NAME, ALL_COLUMNS, null, null, null,
					null, null);
			cursor.moveToFirst();
			while (!cursor.isAfterLast()) {
				if(cursor.getString(13).equals(info.getId())) {
					db.delete(TABLE_NAME, _ID + "=?", new String[]{cursor.getString(15)});
					return true;
				}
				cursor.moveToNext();
			}			
			return false;
		} catch (SQLException e) {
			return false;
		} finally {
			db.close();
		}
	}

	public Information cursorToInfo(Cursor cursor) {
		Information info = new Information();
		info.setTitle(cursor.getString(0));
		info.setYear(cursor.getString(1));
		info.setRated(cursor.getString(2));
		info.setReleased(cursor.getString(3));
		info.setRuntime(cursor.getString(4));
		info.setGenre(cursor.getString(5));
		info.setDirector(cursor.getString(6));
		info.setWriter(cursor.getString(7));
		info.setActors(cursor.getString(8));
		info.setPlot(cursor.getString(9));
		info.setImageUrl(cursor.getString(10));
		info.setRating(cursor.getString(11));
		info.setVotes(cursor.getString(12));
		info.setId(cursor.getString(13));
		info.setType(cursor.getString(14));
		return info;
	}

	public ContentValues infoToValues(Information info) {
		ContentValues values = new ContentValues();
		values.put(NAME_TITLE, info.getTitle());
		values.put(NAME_YEAR, info.getYear());
		values.put(NAME_RATED, info.getRated());
		values.put(NAME_RELEASED, info.getReleased());
		values.put(NAME_RUNTIME, info.getRuntime());
		values.put(NAME_GENRE, info.getGenre());
		values.put(NAME_DIRECTOR, info.getDirector());
		values.put(NAME_WRITER, info.getWriter());
		values.put(NAME_ACTORS, info.getActors());
		values.put(NAME_PLOT, info.getPlot());
		values.put(NAME_POSTER, info.getImageUrl());
		values.put(NAME_RATING, info.getRating());
		values.put(NAME_VOTES, info.getVotes());
		values.put(NAME_ID, info.getId());
		values.put(NAME_TYPE, info.getType());
		return values;
	}
}
